#ifndef MESSAGE_PROTOCOL_APPLICATION_H_
#define MESSAGE_PROTOCOL_APPLICATION_H_

#include "DRV922xx_trapezoidal_sensored.h"
#include "message_protocol.h"
#include "monterey_uart.h"

// Default Values

#define DEVICE_ID							1 // 1
#define FW_VERSION							0x000b		// 0.11

#define DEFAULT_OFF_THROTTLE				7
#define DEFAULT_MAX_NONCOMPL_CURRENT		0
#define DEFAULT_NONCOMP_HYSTERESIS			0
// 3 Amps : 512 counts = 3 Amps x 0.1 ohm * 4 V/V gain * 1024 counts / 2.4 V
#define IFILT_3AMP_0p1OHM_4GAIN			512
#define IFILT_2p2AMP_0p1OHM_4GAIN		375//215
#define DEFAULT_IFILT_MAX					IFILT_3AMP_0p1OHM_4GAIN//800 //256	//435    //358 MH //563 sensorless
#define DEFAULT_IFILT_MID					IFILT_2p2AMP_0p1OHM_4GAIN//215 //290    //256 MH // 460 // sensorless
#define DEFAULT_DEAD_TIME					100			// 4us

#define DEFAULT_ADMIN_STATE					0
#define DEFAULT_DEAD_TIME					100								/*!< 4us */

#ifdef DRV92250
#define DEFAULT_HW_OC						(DEFAULT_AFE_REG_DACB & 0xFF)
#else
#define DEFAULT_HW_OC						(((DEFAULT_AFE_REG_DAC1 << 8) | DEFAULT_AFE_REG_DAC2) & 0x03FF)
#endif

#define DEFAULT_I_MAX_NONCMPL_PWM			0//1023							/*!< Maximum current threshold for non-complementary mode */
#define DEFAULT_I_NONCMPL_PWM_HYS			0								/*!< Maximum current threshold for non-complementary mode hysteresis */

#define DEFAULT_THROTTLE_MIN_V				405								/*!< Minimum throttle before turning on PWM */

#define DEFAULT_PWM_LOW_UPPER_BOUND			75								/*!< PWM low range upper bound [6us] */
#define DEFAULT_PWM_MID_UPPER_BOUND			150								/*!< PWM mid range upper bound [12us] */
#define DEFAULT_PWM_MIN_DUTY				39									/*!< PWM minimum duty cycle [5%] */

// Protocol command enumerations

enum CommandIdApplication
{ 
	// Configuration (read/write)
	
	CmdMsgAdminState = 50,
	CmdDeadTime,
	CmdHwOc,
	CmdIfilterMax,
	CmdIfilterMid,
	CmdImaxNonComplementaryPwm,
	CmdINonComplementaryPwmHysteresis,
	CmdThrottleMinVoltage,
	CmdPwmLowUpperBound,
	CmdPwmMidUpperBound,
	CmdPwmMinDuty,
	CmdThrottleOff,

	
	
	// Status (read-only)
	CmdStatus = 100,
	CmdSpeed,
	CmdThrottleVoltage,
	CmdThrottleVoltageFiltered,
	CmdCurrent,
	CmdDutyRange,
	CmdPwmDuty,
	CmdMotorState,
	CmdFanOff,
	CmdDRVcommState,
	
	cmdMsg_fan_stop,
	cmdMsg_fan_speed_up,
	cmdMsg_fan_speed_down,
	cmdMsg_fan_dir_cw,
	cmdMsg_fan_dir_ccw,
	cmdMsg_fan_dir_change,
	cmdMsg_breakSynch
	
};

enum StatusBits
{
	StatusInit = 			0x0001,
	StatusBrakeOn = 		0x0002,
	StatusHwOc = 			0x0004,
	StatusCmplMode = 		0x0008,
	StatusReverse = 		0x0010,
	StatusBemf		=		0x0020
};


// Application Data 

extern unsigned int AppAdminState;					/*!< Administrative state (bits defined in AppAdminStateBits */
extern unsigned int AppDeadTime;					/*!< Dead-time for complementary PWM mode, in 25MHz CCR counts */
extern unsigned int AppHwOc;						/*!< Hardware over-current threshold, in 10b ADC counts */
extern unsigned int AppIfilterMax;					/*!< Maximum current threshold before system starts duty cycle limiting */
extern unsigned int AppIfilterMid;					/*!< Maximum current threhold before system starts holding duty cycle */
extern unsigned int AppImaxNonCmpl;					/*!< Current threshold for switching from non-complementary to complementary PWM */
extern unsigned int AppImaxNonCmplHys;				/*!< Hysteresis current used for switching back from complementary to non-complementary PWM */
extern unsigned int AppThrottleMinVoltage;			/*!< Minimum throttle voltage that must be reached before controller attempts to drive PWM */
extern unsigned int AppPwmLowUpperBound;			/*!< Upper bound of low PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
extern unsigned int AppPwmMidUpperBound;			/*!< Upper bound of mid PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
extern unsigned int AppPwmMinDuty;					/*!< Minimum PWM duty cycle */
extern unsigned int AppOffThrottle;




extern unsigned int AppStatus;						/*!< 16b status word defined by StatusBits enumeration */
extern unsigned int AppSpeed;						/*!< 1ms timer ticks for last sampled 60-degree rotation of motor */
extern unsigned int AppThrottleVoltage;				/*!< last sampled throttle voltage */
extern unsigned int AppThrottleVoltageFiltered;		/*!< last calculated filtered throttle voltage */
extern unsigned int AppTemperature;
extern unsigned int AppThrottleVoltage_Reference;
extern unsigned int AppCurrent;					/*!< last sampled maximum current */
extern unsigned int AppCurrentFiltered;					/*!< last sampled maximum current */
extern unsigned int AppDutyRange;					/*!< PWM duty cycle range (used for sample measurement points) */
extern unsigned int AppPwmDuty;						/*!< PWM duty cycle in 25MHz ticks */
extern unsigned int AppMotorState;					/*!< Motor state according to MotorState above */
extern unsigned int AppFanOff;
extern unsigned int AppOverCurrent;

extern unsigned char uart_transmitting_message; 
extern void send_breakSynch_ACK();
extern void send_dataStream(unsigned char _cmd);
extern void send_cmd(unsigned char _cmd, unsigned int _data);


#endif /*MESSAGE_PROTOCOL_APPLICATION_H_*/
